﻿// --------------------------------
// <copyright file="TableDefinitionTest.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using System.Data;
using System.Globalization;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tronsoft.Actucal.Tables.Test
{        
    /// <summary>
    ///This is a test class for TableDefinitionTest and is intended
    ///to contain all TableDefinitionTest Unit Tests
    ///</summary>
    [TestClass()]
    public class TableDefinitionTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for Columns
        ///</summary>
        [TestMethod()]
        public void Should_Add_Column()
        {
            const string COLUMN_NAME = "Col1";
            Type TYPE = typeof(int);
            
            DataTable table = new DataTable();
           
            table.Columns.Add(COLUMN_NAME, TYPE);

            TableDefinition target = new TableDefinition(table); 
            TableColumnCollection actual = target.Columns;
            
            Assert.AreEqual(1, actual.Count);

            Assert.AreEqual(COLUMN_NAME, actual[0].ColumnName);
            Assert.AreEqual(TYPE, actual[0].DataType);

            Assert.AreEqual(COLUMN_NAME, actual[COLUMN_NAME].ColumnName);
            Assert.AreEqual(TYPE, actual[COLUMN_NAME].DataType);
        }

        /// <summary>
        ///A test for TableDefinition Constructor
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Should_Not_Be_Able_To_Pass_Null_To_Ctor()
        {
            DataTable table = null; 
            TableDefinition target = new TableDefinition(table);
        }

        [TestMethod()]
        public void Should_Execute_Ctor()
        {
            DataTable table = new DataTable("test");
            TableDefinition target = new TableDefinition(table);
        }

        /// <summary>
        ///A test for TableName
        ///</summary>
        [TestMethod()]
        public void Should_Return_The_TableName()
        {
            const string TABLE_NAME = "test";

            DataTable table = new DataTable(TABLE_NAME); 
            TableDefinition target = new TableDefinition(table); 
            string actual = target.TableName;

            Assert.AreEqual(TABLE_NAME, actual);
        }

        [TestMethod()]
        public void Should_Add_A_Table_Row()
        {
            TableManager tableManager = new TableManager();
            TableDefinition table = tableManager.Tables.Add("table1");

            table.Columns.Add("Col1", typeof(int));
            table.Columns.Add("Col2", typeof(string));
            table.Columns.Add("Col3", typeof(string));
            table.Columns.Add("Col4", typeof(double));

            table.AddTableRow(1, "col2", "col3", 1.5);

            Assert.AreEqual(1, table.Rows.Count);
        }

        [TestMethod()]
        [ExpectedException(typeof(TablesException), "No colums defined. Define columns before adding a row.")]
        public void Should_Not_Be_Able_To_Add_A_Table_Row_If_No_Columns_Are_Defined()
        {
            TableManager tableManager = new TableManager();
            TableDefinition table = tableManager.Tables.Add("table1");

            table.AddTableRow(1);
        }

        [TestMethod()]
        [ExpectedException(typeof(TablesException), "No colums defined. Define columns before adding a row.")]
        public void Should_Not_Be_Able_To_Add_A_Table_Row_With_A_Stream_If_No_Columns_Are_Defined()
        {
            string table1Data =
    @"Col1, Col2, Col3, Col4
1,""data1 row1"", 1.2, ""data4 row 1""
17777,""data1 row2"", 23.78, ""data4 row2""";

            TableManager tableManager = new TableManager();
            TableDefinition table = tableManager.Tables.Add("table1");

            table.AddTableRows(new StringReader(table1Data), true, CultureInfo.InvariantCulture);
        }

        [TestMethod()]
        public void Should_Add_A_Table_Row_With_A_Stream()
        {
            string table1Data =
                @"Col1, Col2, Col3, Col4
1,""data1 row1"", 1.2, ""data4 row 1""
17777,""data1 row2"", 23.78, ""data4 row2""";

            TableManager tableManager = new TableManager();
            TableDefinition table = tableManager.Tables.Add("table1");

            table.Columns.Add("Col1", typeof(int));
            table.Columns.Add("Col2", typeof(string));
            table.Columns.Add("Col3", typeof(double));
            table.Columns.Add("Col4", typeof(string));

            table.AddTableRows(new StringReader(table1Data), true, CultureInfo.InvariantCulture);

            Assert.AreEqual(2, table.Rows.Count);
        }

        [TestMethod()]
        public void Should_Be_Able_To_Index_A_Row()
        {
            TableManager tableManager = new TableManager();
            TableDefinition table = tableManager.Tables.Add("table1");

            table.Columns.Add("Col1", typeof(int));
            table.Columns.Add("Col2", typeof(string));
            table.Columns.Add("Col3", typeof(string));
            table.Columns.Add("Col4", typeof(double));

            table.AddTableRow(1, "col2", "col3", 1.5);

            Assert.AreEqual(1, table.Rows.Count);

            Assert.AreEqual(1, table.Rows[0][0]);
            Assert.AreEqual("col2", table.Rows[0][1]);
            Assert.AreEqual("col3", table.Rows[0][2]);
            Assert.AreEqual(1.5, table.Rows[0][3]);

            Assert.AreEqual(1, table[0, 0]);
            Assert.AreEqual("col2", table[0, 1]);
            Assert.AreEqual("col3", table[0, 2]);
            Assert.AreEqual(1.5, table[0, 3]);

            Assert.AreEqual(1, table[0, "Col1"]);
            Assert.AreEqual("col2", table[0, "Col2"]);
            Assert.AreEqual("col3", table[0, "Col3"]);
            Assert.AreEqual(1.5, table[0, "Col4"]);
        }

        [TestMethod()]
        [ExpectedException(typeof(IndexOutOfRangeException))]
        public void Should_Not_Be_Able_To_Index_A_Row_With_Negative_RowIndex()
        {
            TableManager tableManager = new TableManager();
            TableDefinition table = tableManager.Tables.Add("table1");

            table.Columns.Add("Col1", typeof(int));

            table.AddTableRow(1);

            Assert.AreEqual(1, table[-1, "Col1"]);
        }

        [TestMethod()]
        [ExpectedException(typeof(IndexOutOfRangeException))]
        public void Should_Not_Be_Able_To_Index_A_Row_With_Negative_ColumnIndex()
        {
            TableManager tableManager = new TableManager();
            TableDefinition table = tableManager.Tables.Add("table1");

            table.Columns.Add("Col1", typeof(int));

            table.AddTableRow(1);

            Assert.AreEqual(1, table[0, -1]);
        }

        [TestMethod()]
        [ExpectedException(typeof(ArgumentException))]
        public void Should_Not_Be_Able_To_Index_A_Row_With_Empty_ColumnName()
        {
            TableManager tableManager = new TableManager();
            TableDefinition table = tableManager.Tables.Add("table1");

            table.Columns.Add("Col1", typeof(int));

            table.AddTableRow(1);

            Assert.AreEqual(1, table[0, ""]);
            Assert.AreEqual(1, table[0, null]);
        }

        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Should_Not_Be_Able_To_Index_A_Row_With_Null_ColumnName()
        {
            TableManager tableManager = TableManager.Instance;
            tableManager.Clear();

            TableDefinition table = tableManager.Tables.Add("table1");

            table.Columns.Add("Col1", typeof(int));

            table.AddTableRow(1);

            Assert.AreEqual(1, table[0, null]);
        }

        [TestMethod]
        public void Should_Clear_Table()
        {
            TableManager tableManager = new TableManager();
            TableDefinition table = tableManager.Tables.Add("table1");

            table.Columns.Add("Col1", typeof(int));
            table.AddTableRow(1);

            table.Clear();

            Assert.AreEqual(0, table.Rows.Count);
        }
    }
}
